package com.qf.sy2103.ssm.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.sy2103.ssm.Dao.ClientDao;
import com.qf.sy2103.ssm.Dao.UserDao;
import com.qf.sy2103.ssm.Service.ClientService;
import com.qf.sy2103.ssm.entity.Client;
import com.qf.sy2103.ssm.vo.Clientvo;
import com.qf.sy2103.ssm.vo.Tablevo;
import com.qf.sy2103.ssm.vo.pagevo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    private ClientDao clientDao;
    //返回分页数据的方法
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Tablevo<Client> getAll(pagevo pagevo, Client client) {

        PageHelper.startPage(pagevo.getPage(),pagevo.getLimit());
        List<Client> list =  clientDao.getAllClient(client);
        PageInfo<Client> clientPageInfo = new PageInfo<>(list);
        return new Tablevo<Client>(0,"ok",clientPageInfo.getTotal(),list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addClient(Client client) {
        int i = clientDao.addClient(client);
        if (i==0){
            return false;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteClient(List<Integer> ids) {
        int a = clientDao.deleteClient(ids);
        if (a==0){
            return false;
        }
        return true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Client selectCilent(String username) {
        Client client = clientDao.seleteClient(username);
        return client;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateClient(Client client) {
        int i = clientDao.updateCilent(client);
        if (i==0){
            return false;
        }
        return true;
    }


}
